# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/9/7 5:26 下午
# @Author  : 白辉东
# @Mail    : masterbai2018@outlook.com
# @File    : merge_log.py
# @Software: PyCharm

import os
import os.path


root_dir = os.path.dirname(__file__)


def get_log_list():
    file_list = []
    logs_dir = os.path.join(root_dir, 'log')
    for file in os.listdir(logs_dir):
        if any(key in file for key in ['log', 'txt']):
            file_list.append(file)
    return sorted(file_list)


def merge_logs(file_list):
    print(f'正在合并{len(file_list)}个日志文件！')
    all_logs_file = open('./all.log', 'a+', encoding='UTF-8')

    for log_file in file_list:
        file = open(os.path.join(root_dir, 'log', log_file), 'r', encoding='UTF-8')
        for buffer in file:
            all_logs_file.write(buffer)
        file.close()
    all_logs_file.close()
    print('合并完成！')


def main():
    log_list = get_log_list()
    merge_logs(log_list)


if __name__ == '__main__':
    main()
